package org.zoffy.service.hotTopic.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.zoffy.config.HotTopicConfig;
import org.zoffy.model.entity.HotTopicItem;
import org.zoffy.model.vo.HotTopicVO;
import org.zoffy.service.hotTopic.HotTopicService;

import java.util.List;
import org.springframework.core.ParameterizedTypeReference;

@Service
public class HotTopicServiceImpl implements HotTopicService {
    private static final String DOUYIN_CACHE_KEY = "'douyinHotTopics'";
    private static final String WEIBO_CACHE_KEY = "'weiboHotTopics'";
    private static final String BAIDU_CACHE_KEY = "'baiduHotTopics'";

    @Autowired
    private HotTopicConfig hotTopicConfig;
    
    @Autowired
    private RestTemplate restTemplate;

    @Override
    @Cacheable(value = "hotTopicsCache", key = DOUYIN_CACHE_KEY, unless = "#result == null")
    public HotTopicVO getDouyinHotTopics() {
        ResponseEntity<HotTopicVO> response = restTemplate.exchange(
            hotTopicConfig.getDouyinUrl(), HttpMethod.GET,null,new ParameterizedTypeReference<HotTopicVO>(){}
        );
        return response.getBody();
    }

    @Override
    @Cacheable(value = "hotTopicsCache", key = WEIBO_CACHE_KEY, unless = "#result == null")
    public HotTopicVO getWeiboHotTopics() {
        ResponseEntity<HotTopicVO> response = restTemplate.exchange(
                hotTopicConfig.getWeiboUrl(), HttpMethod.GET,null,new ParameterizedTypeReference<HotTopicVO>(){}
        );
        return response.getBody();
    }

    @Override
    @Cacheable(value = "hotTopicsCache", key = BAIDU_CACHE_KEY, unless = "#result == null")
    public HotTopicVO getBaiduHotTopics() {
        ResponseEntity<HotTopicVO> response = restTemplate.exchange(
                hotTopicConfig.getBaiduUrl(), HttpMethod.GET,null,new ParameterizedTypeReference<HotTopicVO>(){}
        );
        return response.getBody();
    }

}
